package com.service.caseoneservice.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.controller.vo.CaseVO;
import com.mapper.AccidentMapper;
import com.mapper.CaseDiscribeMapper;
import com.mapper.CaseStatusMapper;
import com.mapper.CaseoneMapper;
import com.mapper.EmployeeMapper;
import com.mapper.OwnCheckImgMapper;
import com.pojo.Accident;
import com.pojo.AccidentExample;
import com.pojo.CaseDiscribe;
import com.pojo.CaseDiscribeExample;
import com.pojo.CaseStatus;
import com.pojo.CaseStatusExample;
import com.pojo.Caseone;
import com.pojo.CaseoneExample;
import com.pojo.CaseoneExample.Criteria;
import com.pojo.Employee;
import com.pojo.OwnCheckImg;
import com.pojo.OwnCheckImgExample;
import com.service.caseoneservice.Caseoneservice;

import net.sf.jsqlparser.expression.CaseExpression;
@Service
public class CaseoneserviceImpl implements Caseoneservice{
    @Autowired
    private CaseoneMapper  caseoneMapper;
    @Autowired
    private CaseDiscribeMapper caseDiscribeMapper;
	@Autowired
	private AccidentMapper  accidentMapper;
	@Autowired
	private CaseStatusMapper caseStatusMapper;
	@Autowired
	private OwnCheckImgMapper  ownCheckImgMapper;
	@Autowired
	private EmployeeMapper employeeMapper;
	
	@Override
	public List<CaseVO> getCaseVOList() {
		//初始化一个caseVO类的集合
		List<CaseVO> caseVOList=new ArrayList<CaseVO>();
		//查找状态为审核中的案件处理集合
		CaseoneExample caseoneExample = new CaseoneExample();
		Criteria criteria = caseoneExample.createCriteria();
		criteria.andStatusidEqualTo(4);
		List<Caseone> list = caseoneMapper.selectByExample(caseoneExample);
		//判断集合是否存在
		if (list!=null&&list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				
				CaseVO caseVO = new CaseVO();
				//循环得到当前案件处理对象
				Caseone caseone = list.get(i);
				//得到案件处理单号
				String caseno = caseone.getCaseno();
				//得到案件状态
				Integer statusid = caseone.getStatusid();
				CaseStatusExample caseStatusExample = new CaseStatusExample();
				com.pojo.CaseStatusExample.Criteria criteria4 = caseStatusExample.createCriteria();
				criteria4.andStatusidEqualTo(statusid);
				List<CaseStatus> list4 = caseStatusMapper.selectByExample(caseStatusExample);
				CaseStatus caseStatus = list4.get(0);
				String status = caseStatus.getStatus();
				//得到处理员工id
				Integer employeeid = caseone.getEmployeeid();
				Employee employee = employeeMapper.selectByPrimaryKey(employeeid);
				//得到用户号码
				String userphone = caseone.getUserphone();
				
				
				//得到当前案件处理对象的案件描述
				CaseDiscribeExample caseDiscribeExample = new CaseDiscribeExample();
				com.pojo.CaseDiscribeExample.Criteria criteria2 = caseDiscribeExample.createCriteria();
				criteria2.andCasenoEqualTo(caseno);
				List<CaseDiscribe> list2 = caseDiscribeMapper.selectByExample(caseDiscribeExample);
				CaseDiscribe caseDiscribe=null;
				if (list2!=null&&list2.size()>0) {
				     caseDiscribe = list2.get(0);
				}
				Integer casualtystatus = caseDiscribe.getCasualtystatus();
				Integer iscarnormal = caseDiscribe.getIscarnormal();
				Integer ishere = caseDiscribe.getIshere();
				Integer isone = caseDiscribe.getIsone();
				Integer accidentid = caseDiscribe.getAccidentid();
				String insuranceaddress = caseDiscribe.getInsuranceaddress();
				String carnumber = caseDiscribe.getCarnumber();
				String driver = caseDiscribe.getDriver();
				Date insurancetime = caseDiscribe.getInsurancetime();
				String casephone = caseDiscribe.getCasephone();
				String insuredname = caseDiscribe.getInsuredname();
				String identity = caseDiscribe.getIdentity();
				
				AccidentExample accidentExample = new AccidentExample();
				com.pojo.AccidentExample.Criteria criteria3 = accidentExample.createCriteria();
				criteria3.andAccidentidEqualTo(accidentid);
				List<Accident> list3 = accidentMapper.selectByExample(accidentExample);
				Accident accident=null;
				if (list3!=null&&list3.size()>0) {
					accident = list3.get(0);
				}
				String accidenttype = accident.getAccidenttype();
				
				OwnCheckImgExample ownCheckImgExample = new OwnCheckImgExample();
				com.pojo.OwnCheckImgExample.Criteria criteria5 = ownCheckImgExample.createCriteria();
				criteria5.andCasenoEqualTo(caseno);
				List<OwnCheckImg> list5 = ownCheckImgMapper.selectByExample(ownCheckImgExample);
				
				caseVO.setCaseno(caseno);
			
				caseVO.setStatusid(statusid);
				caseVO.setStatus(status);
				caseVO.setUserphone(userphone);
				caseVO.setCasualtystatus(casualtystatus);
				caseVO.setIscarnormal(iscarnormal);
				caseVO.setIshere(ishere);
				caseVO.setIsone(isone);
				caseVO.setAccidentid(accidentid);
				caseVO.setAccidenttype(accidenttype);
				caseVO.setInsurancetime(insurancetime);
				caseVO.setInsuranceaddress(insuranceaddress);
				caseVO.setCarnumber(carnumber);
				caseVO.setDriver(driver);
				caseVO.setCasephone(casephone);
				caseVO.setInsuredname(insuredname);
				caseVO.setIdentity(identity);
				caseVO.setImgList(list5);
				
				caseVOList.add(caseVO);
			}
		}
		
		
		
		return caseVOList;
	}

	@Override
	public CaseVO getCaseVO(String caseno,Employee employee) {
		CaseVO caseVO = new CaseVO();
		CaseoneExample caseoneExample = new CaseoneExample();
		Criteria criteria = caseoneExample.createCriteria();
		criteria.andCasenoEqualTo(caseno);
		List<Caseone> list = caseoneMapper.selectByExample(caseoneExample);
		Caseone caseone=null;
		if (list!=null&&list.size()>0) {
			caseone=list.get(0);
		}
		
		String userphone = caseone.getUserphone();
		Integer statusid = caseone.getStatusid();
		CaseStatusExample caseStatusExample = new CaseStatusExample();
		com.pojo.CaseStatusExample.Criteria criteria2 = caseStatusExample.createCriteria();
		criteria2.andStatusidEqualTo(statusid);
		List<CaseStatus> list2 = caseStatusMapper.selectByExample(caseStatusExample);
		CaseStatus caseStatus=null;
		if (list2!=null&&list2.size()>0) {
			caseStatus=list2.get(0);
		}
		String status = caseStatus.getStatus();
		
		
		CaseDiscribeExample caseDiscribeExample = new CaseDiscribeExample();
		com.pojo.CaseDiscribeExample.Criteria criteria3 = caseDiscribeExample.createCriteria();
		criteria3.andCasenoEqualTo(caseno);
		List<CaseDiscribe> list3 = caseDiscribeMapper.selectByExample(caseDiscribeExample);
		CaseDiscribe caseDiscribe=null;
		if (list3!=null&&list3.size()>0) {
			caseDiscribe=list3.get(0);
		}
		Integer casualtystatus = caseDiscribe.getCasualtystatus();
		Integer iscarnormal = caseDiscribe.getIscarnormal();
		Integer ishere = caseDiscribe.getIshere();
		Integer isone = caseDiscribe.getIsone();
		Integer accidentid = caseDiscribe.getAccidentid();
		Accident accident = accidentMapper.selectByPrimaryKey(accidentid);
		String accidenttype = accident.getAccidenttype();
		Date insurancetime = caseDiscribe.getInsurancetime();
		String insuranceaddress = caseDiscribe.getInsuranceaddress();
		String carnumber = caseDiscribe.getCarnumber();
		String driver = caseDiscribe.getDriver();
		String casephone = caseDiscribe.getCasephone();
		String insuredname = caseDiscribe.getInsuredname();
		String identity = caseDiscribe.getIdentity();
		
		OwnCheckImgExample ownCheckImgExample = new OwnCheckImgExample();
		com.pojo.OwnCheckImgExample.Criteria criteria4 = ownCheckImgExample.createCriteria();
		criteria4.andCasenoEqualTo(caseno);
		List<OwnCheckImg> list4 = ownCheckImgMapper.selectByExample(ownCheckImgExample);
		
		caseVO.setCaseno(caseno);
		caseVO.setEmployee(employee);
		caseVO.setStatusid(statusid);
		caseVO.setStatus(status);
		caseVO.setUserphone(userphone);
		caseVO.setCasualtystatus(casualtystatus);
		caseVO.setIscarnormal(iscarnormal);
		caseVO.setIshere(ishere);
		caseVO.setIsone(isone);
		caseVO.setAccidentid(accidentid);
		caseVO.setAccidenttype(accidenttype);
		caseVO.setInsurancetime(insurancetime);
		caseVO.setInsuranceaddress(insuranceaddress);
		caseVO.setCarnumber(carnumber);
		caseVO.setDriver(driver);
		caseVO.setCasephone(casephone);
		caseVO.setInsuredname(insuredname);
		caseVO.setIdentity(identity);
		caseVO.setImgList(list4);
		
		return caseVO;
	}

	@Override
	public Caseone selectByPrimaryKey(Integer caseid) {
		
		return caseoneMapper.selectByPrimaryKey(caseid);
	}

	@Override
	public int updateByPrimaryKey(Caseone record) {
		
		return caseoneMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<Caseone> selectByExample(CaseoneExample example) {
		
		return caseoneMapper.selectByExample(example);
	}

	@Override
	public int updateByExampleSelective(Caseone record, CaseoneExample example) {
		
		return caseoneMapper.updateByExample(record, example);
	}

}
